package com.zhuchaooasystem.presenter.home.serviceunusual;

import android.app.Activity;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.HttpUtil;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.ServiceUnusualEntity;
import com.zhuchaooasystem.entity.UnusualDeleteEntity;
import com.zhuchaooasystem.entity.UnusualDetailEntity;
import com.zhuchaooasystem.utils.ToastUtil;

import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * @author yinsi
 */


public class ServiceUnusualPresenter extends BasePresenter<ServiceUnusualContract.View> implements ServiceUnusualContract.Presenter {

    public ServiceUnusualPresenter(Activity activity, ServiceUnusualContract.View view) {
        super(activity, view);
    }

    @Override
    public void getUnusualList() {
        //----
        int projectId = mView.getProjectId();
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("ProjectId", projectId + "");
        //----
        Subscription subscription = HttpFactory.getHttpApi().getExceptionSpecificationByFlow(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            ServiceUnusualEntity entity = (ServiceUnusualEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.refreshData(entity.getResult().getSuccessful());
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    @Override
    public void getUnusualDetail(int unusualId) {
        showLoading();
        //----
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("ID", unusualId + "");
        //----
        Subscription subscription = HttpFactory.getHttpApi().getExceptionSpecifcationByDetai(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            UnusualDetailEntity entity = (UnusualDetailEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.detailSuccuss(entity.getResult().getSuccessful());
                            } else if (null != entity.getResult().getError()) {
                                handleError(entity.getResult().getError());
                            }
                        }
                        LoadingDiaogDismiss();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        LoadingDiaogDismiss();
                    }
                });
        addSubscrebe(subscription);
    }

    @Override
    public void deleteUnusual(int unusualId) {
        showLoading();
        //----
        final Map<String, String> map = HttpUtil.getHttpMap(mActivity);
        map.put("ID", unusualId + "");
        //----
        Subscription subscription = HttpFactory.getHttpApi().deleteExceptionSpecifcation(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult()).subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {
                        if (null != o) {
                            UnusualDeleteEntity entity = (UnusualDeleteEntity) o;
                            if (null != entity.getResult().getSuccessful()) {
                                mView.deleteSuccuss();
                            } else if (null != entity.getResult().getError()) {
                                ToastUtil.showShort(mActivity, entity.getResult().getError());
                            }
                        }
                        LoadingDiaogDismiss();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        handleError(throwable);
                        LoadingDiaogDismiss();
                    }
                });
        addSubscrebe(subscription);
    }
}
